 
package com.xbongbong.admin.model.impl;

import com.xbongbong.admin.domain.dao.RaidersGroupDao;
import com.xbongbong.admin.domain.entity.RaidersGroupEntity;
import com.xbongbong.admin.model.RaidersGroupModel;
import com.xbongbong.admin.pojo.RaidersNormalPojo;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.constant.RaidersConstant;
import com.xbongbong.pro.enums.raiders.enums.RaidersGroupRecommendEnum;
import com.xbongbong.saas.constant.BasicConstant;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;


/**
* @author autoGen
* @time 2019-09-18 13:56:16
*/
@Service("raidersGroupModel")
public class RaidersGroupModelImpl implements RaidersGroupModel {

	@Resource
	private RaidersGroupDao dao;

	@Override
	public Integer insert(RaidersGroupEntity entity){
		long now = DateUtil.getInt();
		entity.setAddTime(now);
		entity.setUpdateTime(now);
		
		return dao.insert(entity);
	}

	@Override
	public Integer update(RaidersGroupEntity entity){
		long now = DateUtil.getInt();
		entity.setUpdateTime(now);
		
		return dao.update(entity);
	}

	@Override
	public Integer save(RaidersGroupEntity entity){
		
		if(entity.getId() == null || Objects.equals(0L, entity.getId())) {
			return insert(entity);
		}
		return update(entity);
	}

	@Override
	public void insertBatch(List<RaidersGroupEntity> entityList) {
    	dao.insertBatch(entityList);
    }

	@Override
	public Integer deleteByKey( Long key){
		return dao.deleteByKey(key);
	}

	@Override
	public RaidersGroupEntity getByKey( Long key){
		return dao.getByKey(key);
	}

	@Override
	public List<RaidersGroupEntity> findEntitys(Map<String ,Object>  param){
		return dao.findEntitys(param);
	}

    @Override
	public Integer getEntitysCount(Map<String ,Object>  param){
		return dao.getEntitysCount(param);
	}

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
	public Map<String, Object> validListMap(boolean sortFlag) {
		Map<String, Object> groupMap = new HashMap<>(4);
		groupMap.put("del", DelEnum.NORMAL.getDel());
		groupMap.put("enable", BasicConstant.IS_USE);
		if (sortFlag) {
			groupMap.put("orderByStr", RaidersConstant.ORDER_BY_STR_LIST);
			groupMap.put("columns", "id,name");
		}
		return groupMap;
	}

	@Override
	public List<RaidersGroupEntity> getValidListList(boolean sortFlag) {
		//获得分组列表
		Map<String, Object> groupMap = validListMap(sortFlag);
		return findEntitys(groupMap);
	}

	@Override
	public void updateBatch(List<RaidersGroupEntity> list) {
		dao.updateBatch(list);
	}

	@Override
	public List<RaidersNormalPojo> handleGroupList2PojoList(List<RaidersGroupEntity> groupList, Integer index) {
		//分组list（只获取到分组的部分属性）
		List<RaidersNormalPojo> groupPojoList = new ArrayList<>();
		for (RaidersGroupEntity entity : groupList) {
			RaidersNormalPojo groupPojo = new RaidersNormalPojo(entity.getId(), entity.getName());
			groupPojoList.add(groupPojo);
		}
		if (index == null) {
		    return groupPojoList;
		} else if (index == 0) {
			//推荐分组
			RaidersNormalPojo recommend = new RaidersNormalPojo(RaidersGroupRecommendEnum.RECOMMEND.getId(), RaidersGroupRecommendEnum.RECOMMEND.getName());
		    groupPojoList.add(0, recommend);
		} else {
			//推荐分组
			RaidersNormalPojo recommend = new RaidersNormalPojo(RaidersGroupRecommendEnum.RECOMMEND.getId(), RaidersGroupRecommendEnum.RECOMMEND.getName());
			groupPojoList.add(recommend);
		}
		return groupPojoList;
	}

}

 